package com.rain.cy.business.service;

import cn.hutool.core.collection.CollectionUtil;
import com.rain.cy.business.constant.CommonConstant;
import com.rain.cy.business.constant.CountTypeEnum;
import com.rain.cy.business.model.dto.CountDataDTO;
import com.rain.cy.business.model.entity.DictionaryEntity;
import com.rain.cy.business.model.entity.DrugCountEntity;
import com.rain.cy.business.model.entity.UserEntity;
import com.rain.cy.business.model.entity.UserFeedbackEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yu_chen
 * @since 2021/10/7 16:00
 */
@Service
public class CountService {


    @Autowired
    private UserService userService;


    @Autowired
    private UserFeedbackService userFeedbackService;

    @Autowired
    private UserProcessStatusService userProcessStatusService;

    @Autowired
    private DrugCountService drugCountService;


    @Autowired
    private DictionaryService dictionaryService;

    public CountDataDTO getDoctorUserProcess(Integer doctorUserId) {
        List<DictionaryEntity> cancerTypeList = dictionaryService.getCancerType();
        CountDataDTO countDataDTO = CountDataDTO.of(cancerTypeList);
        //获取所有患者
        List<UserEntity> patientList = userService.lambdaQuery().eq(UserEntity::getUserType, 1).eq(UserEntity::getDoctorId, doctorUserId).list();
        for (int i = 1; i <= CommonConstant.PROCESS_NUM; i++) {
            for (DictionaryEntity cancerType : cancerTypeList) {
                int currentProcessIndex = i;
                long count = patientList.stream()
                        .filter(userEntity -> userProcessStatusService.getCurrentProcess(userEntity.getId()) >= currentProcessIndex)
                        .filter(userEntity -> StringUtils.equals(userEntity.getCheckType(), cancerType.getItemKey() + ""))
                        .count();
                countDataDTO.getSeriesItemMap().get(cancerType.getItemValue()).getData().add((int) count);
            }
            countDataDTO.getXTypeList().add(i + "");
        }
        return countDataDTO;
    }


    /**
     * 当前住院人数统计
     *
     * @return
     */
    public CountDataDTO getCurrentUserNumInnerOrOuter(Integer userId, boolean isInner) {
        List<DictionaryEntity> legendList = dictionaryService.getDepType();
        List<DictionaryEntity> cancerTypeList = dictionaryService.getCancerType();
        CountDataDTO countDataDTO = CountDataDTO.of(legendList).xTypeList(cancerTypeList);
        if (!isInner) {
            countDataDTO.setType(CountTypeEnum.LINE.getType());
        }
        //获取所有患者
        List<UserEntity> patientList = userService.lambdaQuery()
                .eq(UserEntity::getUserType, 0)
                .eq(UserEntity::getDoctorId, userId)
                .eq(UserEntity::getLeaveStatus, isInner ? 0 : 1)
                .list();
        for (DictionaryEntity legend : legendList) {
            for (DictionaryEntity cancerType : cancerTypeList) {
                long count = patientList.stream()
                        .filter(userEntity -> StringUtils.equals(userEntity.getDepartment(), legend.getItemKey()))
                        .filter(userEntity -> StringUtils.equals(userEntity.getCheckType(), cancerType.getItemKey()))
                        .count();
                countDataDTO.getSeriesItemMap().get(legend.getItemValue()).getData().add((int) count);
            }
        }
        return countDataDTO;
    }


    /**
     * 患者反馈
     *
     * @param userId
     * @return
     */
    public CountDataDTO getUserFeedbackCount(Integer userId) {
        List<DictionaryEntity> legendList = dictionaryService.getUserFeedBackTag();
        List<DictionaryEntity> cancerTypeList = dictionaryService.getCancerType();
        CountDataDTO countDataDTO = CountDataDTO.of(legendList).xTypeList(cancerTypeList);
        //获取所有患者
        List<UserFeedbackEntity> userFeedbackEntities = userFeedbackService.lambdaQuery()
                .eq(UserFeedbackEntity::getDoctorId, userId)
                .list();
        if (CollectionUtil.isEmpty(userFeedbackEntities)) {
            return countDataDTO;
        }
        for (DictionaryEntity legend : legendList) {
            for (DictionaryEntity cancerType : cancerTypeList) {
                long count = userFeedbackEntities.stream()
                        .filter(userFeedbackEntity -> StringUtils.isNotEmpty(userFeedbackEntity.getTag()))
                        .filter(userFeedbackEntity -> StringUtils.equals(userFeedbackEntity.getTag(), legend.getItemKey()))
                        .map(userFeedbackEntity -> userService.getById(userFeedbackEntity.getUserId()))
                        .filter(userEntity -> StringUtils.equals(userEntity.getCheckType(), cancerType.getItemKey()))
                        .count();
                countDataDTO.getSeriesItemMap().get(legend.getItemValue()).getData().add((int) count);
            }
        }
        return countDataDTO;
    }


    /**
     * 药品存量统计
     *
     * @param userId 医生ID
     * @return
     */
    public CountDataDTO getDrugLeftCount(Integer userId) {
        List<DictionaryEntity> legendList = dictionaryService.getDrugType();
        //可以增加用户ID过滤
        List<DrugCountEntity> drugCountEntities = drugCountService.lambdaQuery().orderByAsc(DrugCountEntity::getCreateDay).list();
        List<String> createDays = drugCountEntities.stream()
                .map(DrugCountEntity::getCreateDay)
                .collect(Collectors.toList());
        CountDataDTO countDataDTO = CountDataDTO.of(legendList).xTypeStrList(createDays);
        if (CollectionUtil.isEmpty(drugCountEntities)) {
            return countDataDTO;
        }
        long count = drugCountEntities.stream()
                .map(DrugCountEntity::getDrugDataConfig)
                .flatMap(drugCountDataJson -> drugCountDataJson.getDrugCountItemList().stream())
                .peek(drugCountItem -> countDataDTO.getSeriesItemMap().get(drugCountItem.getDrugType()).getData().add(drugCountItem.getNum()))
                .count();
        return countDataDTO;
    }
}
